package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishDto;
import com.itheima.reggie.entity.R;
import com.itheima.reggie.service.IDishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <b><code>DishController</code></b>
 * <p/>
 * Description
 * <p/>
 * <b>Creation Time:</b> 24/06/2024 16:39.
 *
 * @author yanjr
 * @since reggie_take_out
 */
@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private IDishService dishService;

    /**
     * 菜品分页查询
     * @param page 当前页
     * @param pageSize 每页显示条数
     * @param name 菜品名称
     * @return 分页信息
     */
    @GetMapping("/page")
    public R<Page<Dish>> queryDish(@RequestParam(name = "page") int page,
                                       @RequestParam(name = "pageSize") int pageSize,
                                       @RequestParam(name = "name", required = false, defaultValue = "") String name) {
        return dishService.queryDish(page, pageSize, name);
    }

    /**
     * 新增菜品
     * @param dish 菜品信息
     * @return 是否新增成功
     */
    @PostMapping
    public R<String> addDish(@RequestBody DishDto dish) {
        return dishService.addDish(dish);
    }

    /**
     * 根据id查询菜品信息
     * @param id 菜品id
     * @return 菜品信息
     */
    @GetMapping("/{id}")
    public R<DishDto> queryDishById(@PathVariable("id") Long id) {
        return dishService.queryDishById(id);
    }

    /**
     * 批量起售/停售
     * @param status 状态，起售-1，停售-0
     * @param ids 菜品id，多个以逗号分隔
     * @return 操作是否成功
     */
    @PostMapping("/status/{status}")
    public R<String> changeStatus(@PathVariable("status") Integer status, @RequestParam("ids") String ids) {
        return dishService.changeStatus(status, ids);
    }

    /**
     * 批量删除
     * @param ids 菜品id，多个以逗号分隔
     * @return 操作是否成功
     */
    @DeleteMapping()
    public R<String> delteDishByIds(@RequestParam("ids") String ids) {
        return dishService.delteDishByIds(ids);
    }

    /**
     * 修改菜品
     * @param dishDto 菜品信息
     * @return 操作是否成功
     */
    @PutMapping
    public R<String> updateDish(@RequestBody DishDto dishDto) {
        return dishService.updateDish(dishDto);
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId 分类id
     * @return 菜品信息
     */
    @GetMapping("/list")
    public R<List<DishDto>> queryByCategoryId(@RequestParam("categoryId") Long categoryId) {
//        dishService.getBaseMapper().selectList(new LambdaQueryWrapper<Dish>().eq(Dish::getCategoryId, categoryId));
        List<DishDto> result = dishService.queryByCategoryId(categoryId);
        return R.success(result);
    }
}
